﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SMS.Entities.BaseClasses;
using SMS.Common.Silverlight;
using System.ComponentModel;
using System.Collections.Generic;
using SMS.Entities;
using SMS.Entities.Master;
using System.Windows.Data;

namespace SMS.ViewModel.Master
{
    public class BranchMasterViewModel : BaseNotify
    {
        SMS.Model.branchMasterModel _model;
        public BranchMasterViewModel()
        {
            SearchCommand = new DelegateCommand(Search_Click, null);
            SaveCommand = new DelegateCommand(Save_Click, null);
            NewCommand = new DelegateCommand(New_Click, null);
            if (!DesignerProperties.IsInDesignTool)
            {
                _model = new Model.branchMasterModel();
                _model.OnSearchCompleted += new Action<System.Collections.ObjectModel.ObservableCollection<Branch>>(_model_OnSearchCompleted);
                _model.OnSaveBranchCompleted += new Action<int>(_model_OnSaveBranchCompleted);
            }
        }

        void _model_OnSaveBranchCompleted(int obj)
        {
            UserMessage = "Branch saved successfully.";
            SelectedMaster.Branch_ID = obj;
        }

        void _model_OnSearchCompleted(System.Collections.ObjectModel.ObservableCollection<Branch> obj)
        {
            UserMessage = obj.Count + " Branch found";
            BranchList = new List<Entities.Master.Branch>(obj);
            PagedBranchList = new PagedCollectionView(BranchList);
        }
        private List<Branch> _BranchList;

        public List<Branch> BranchList
        {
            get { return _BranchList; }
            set { _BranchList = value; OnPropertyChanged("BranchList"); }
        }

        private Branch _SelectedBranch;

        public Branch SelectedBranch
        {
            get { return _SelectedBranch; }
            set { _SelectedBranch = value; OnPropertyChanged("SelectedBranch"); }
        }

        private Branch _SelectedMaster;

        public Branch SelectedMaster
        {
            get { return _SelectedMaster; }
            set { _SelectedMaster = value; OnPropertyChanged("SelectedMaster"); }
        }

        private PagedCollectionView _PagedBranchList;

        public PagedCollectionView PagedBranchList
        {
            get { return _PagedBranchList; }
            set { _PagedBranchList = value; OnPropertyChanged("PagedBranchList"); }
        }

        private string _SearchText;

        public string SearchText
        {
            get { return _SearchText; }
            set { _SearchText = value; OnPropertyChanged("SearchText"); }
        }

        private string _Text;

        public string Text
        {
            get { return _Text; }
            set { _Text = value; OnPropertyChanged("Text"); }
        }


        public ICommand SearchCommand { get; private set; }

        public void Search_Click(object param)
        {
            _model.SearchBranch(SearchText);

            UserMessage = "Searching branch...";
        }

        public ICommand SaveCommand { get; set; }

        public void Save_Click(object param)
        {
            if (SelectedMaster != null)
            {

                _model.SaveBranch(SelectedMaster);

                UserMessage = "Saving branch...";
            }
            else
            {
                ErrorMessage = "No branch to save";
            }
        }

        public ICommand NewCommand { get; set; }

        public void New_Click(object param)
        {
            if (BranchList == null)
                BranchList = new List<Branch>();
            var k = new Branch() { IsLive = true };
            BranchList.Add(k);
            PagedBranchList = new PagedCollectionView(BranchList);
            PagedBranchList.MoveToLastPage();
            SelectedMaster = k;
        }
    }
}

